# coding=utf-8
import requests
from src.common.login import jgj_login_data
from src.common.dingding import send_ding
from src.common import log
import unittest
from src.common.readConfig import ReadConfig


class SybTest (unittest.TestCase):
    def setUp(self):
        global jgj_cookie, jgj_readconfig
        jgj_cookie = jgj_login_data ("prod")
        jgj_readconfig = ReadConfig ("prod")
        self.case_name = " "

    def test_loan(self):
        '''贷后测试'''
        try:
            self.loan_url = jgj_readconfig.get_url ("syb_loan_url")
            r1 = requests.get (self.loan_url, cookies=jgj_cookie, timeout=10)
            result = r1.json ()
            print (result)
            self.assertEqual (result["code"], 0)
            if result["code"] == 0:
                log.logger_info.info ("闪佣宝贷后模块正常")
            else:
                send_ding ("闪佣宝贷后模块异常:" + str (result) + "@13682521706", "13682521706")
                log.logger_error.error ("闪佣宝贷后模块异常" + str (result))
        except Exception as ex:
            log.logger_error.error (ex)
            send_ding ("闪佣宝贷后模块异常:" + str (ex) + "@13682521706", "13682521706")
            raise Exception (str (ex))

    def test_order(self):
        '''订单风控测试'''
        try:
            self.order_url = jgj_readconfig.get_url ("syb_order_url")
            r1 = requests.get (self.order_url, cookies=jgj_cookie, timeout=10)
            result = r1.json ()
            print (result)
            self.assertEqual (result["code"], 0)
            if result["code"] == 0:
                log.logger_info.info ("闪佣宝订单风控模块正常")
            else:
                send_ding ("闪佣宝订单风控模块异常:" + str (result) + "@13682521706", "13682521706")
                log.logger_error.error ("闪佣宝订单风控模块异常" + str (result))
        except Exception as ex:
            log.logger_error.error (ex)
            send_ding ("闪佣宝订单风控模块异常:" + str (ex) + "@13682521706", "13682521706")
            raise Exception (str (ex))

    def test_settlement(self):
        '''结算规则测试'''
        try:
            self.settlement_url = jgj_readconfig.get_url ("syb_settlement_url")
            r1 = requests.get (self.settlement_url, cookies=jgj_cookie, timeout=10)
            result = r1.json ()
            print (result)
            self.assertEqual (result["code"], 0)
            if result["code"] == 0:
                log.logger_info.info ("闪佣宝结算规则模块正常")
            else:
                send_ding ("闪佣宝结算规则异常:" + str (result) + "@13682521706", "13682521706")
                log.logger_error.error ("闪佣宝结算规则异常" + str (result))
        except Exception as ex:
            log.logger_error.error (ex)
            send_ding ("闪佣宝结算规则异常:" + str (ex) + "@13682521706", "13682521706")
            raise Exception (str (ex))

    def test_store(self):
        '''商户管理测试'''
        try:
            self.store_url = jgj_readconfig.get_url ("syb_store_url")
            r1 = requests.get (self.store_url, cookies=jgj_cookie, timeout=10)
            result = r1.json ()
            print (result)
            self.assertEqual (result["code"], 0)
            if result["code"] == 0:
                log.logger_info.info ("闪佣宝商户管理模块正常")
            else:
                send_ding ("闪佣宝商户管理模块异常:" + str (result) + "@13682521706", "13682521706")
                log.logger_error.error ("闪佣宝商户管理模块异常" + str (result))
        except Exception as ex:
            log.logger_error.error (ex)
            send_ding ("闪佣宝商户管理模块异常:" + str (ex) + "@13682521706", "13682521706")
            raise Exception (str (ex))

    def test_project(self):
        '''项目管理测试'''
        try:
            self.project_url = jgj_readconfig.get_url ("syb_project_url")
            r1 = requests.get (self.project_url, cookies=jgj_cookie, timeout=10)
            result = r1.json ()
            print (result)
            self.assertEqual (result["code"], 0)
            if result["code"] == 0:
                log.logger_info.info ("闪佣宝项目管理模块正常")
            else:
                send_ding ("闪佣宝项目管理模块异常:" + str (result) + "@13682521706", "13682521706")
                log.logger_error.error ("闪佣宝项目管理模块异常" + str (result))
        except Exception as ex:
            log.logger_error.error (ex)
            send_ding ("闪佣宝项目管理模块异常:" + str (ex) + "@13682521706", "13682521706")
            raise Exception (str (ex))

    def test_payment(self):
        '''付款列表测试'''
        try:
            self.payment_url = jgj_readconfig.get_url ("syb_payment_url")
            r1 = requests.get (self.payment_url, cookies=jgj_cookie, timeout=10)
            result = r1.json ()
            print (result)
            self.assertEqual (result["code"], 0)
            if result["code"] == 0:
                log.logger_info.info ("闪佣宝付款列表模块正常")
            else:
                send_ding ("闪佣宝付款列表模块异常:" + str (result) + "@13682521706", "13682521706")
                log.logger_error.error ("闪佣宝付款列表模块异常" + str (result))
        except Exception as ex:
            log.logger_error.error (ex)
            send_ding ("闪佣宝付款列表模块异常:" + str (ex) + "@13682521706", "13682521706")
            raise Exception (str (ex))

    def test_channel(self):
        '''资金渠道还款测试'''
        try:
            self.channel_url = jgj_readconfig.get_url ("syb_channel_payment_url")
            r1 = requests.get (self.channel_url, cookies=jgj_cookie, timeout=10)
            result = r1.json ()
            print (result)
            self.assertEqual (result["code"], 0)
            if result["code"] == 0:
                log.logger_info.info ("闪佣宝资金渠道还款模块正常")
            else:
                send_ding ("闪佣宝资金渠道还款模块异常:" + str (result) + "@13682521706", "13682521706")
                log.logger_error.error ("闪佣宝资金渠道还款模块异常" + str (result))
        except Exception as ex:
            log.logger_error.error (ex)
            send_ding ("闪佣宝资金渠道还款模块异常:" + str (ex) + "@13682521706", "13682521706")
            raise Exception (str (ex))

    def test_changechannel(self):
        '''修改资金渠道测试'''
        try:
            self.changechannel_url = jgj_readconfig.get_url ("syb_changechannel_url")
            params_add_order = jgj_readconfig.get_body ("syb_changechannel")
            r1 = requests.post (self.changechannel_url, json=params_add_order, cookies=jgj_cookie, timeout=10)
            result = r1.json ()
            print (result)
            self.assertEqual (result["code"], 0)
            if result["code"] == 0:
                log.logger_info.info ("闪佣宝修改资金渠道正常")
            else:
                send_ding ("闪佣宝修改资金渠道异常:" + str (result) + "@13682521706", "13682521706")
                log.logger_error.error ("闪佣宝修改资金渠道异常" + str (result))
        except Exception as ex:
            log.logger_error.error (ex)
            send_ding ("闪佣宝修改资金渠道异常:" + str (ex) + "@13682521706", "13682521706")
            raise Exception (str (ex))
